<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/20
 */

namespace App\Http\Controllers;

use App\Http\Requests\ConfigRequest;
use App\Models\Config;
use App\Services\AttachmentService;
use App\Services\CacheService;

/**
 * 系统配置模块
 * Class ConfigController
 * @package App\Http\Controllers
 */
class ConfigController extends Controller
{

    /**
     * 查看
     *
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        $configs = Config::all();
        $result = [];
        $joinField = ['site' => ['keywords'], 'attachment' => ['attachment_upload_ext'], 'guestbook' => ['keywords']];
        $jsonField = ['site' => ['qrcode_image']];
        collect($configs)->each(function ($item) use (&$result, $joinField, $jsonField) {
            if (isset($joinField[$item->module]) && in_array($item->field, $joinField[$item->module])) {
                $item->field_value = trim($item->field_value) ? explode(',', $item->field_value) : [];
            }
            if (isset($jsonField[$item->module]) && in_array($item->field, $jsonField[$item->module])) {
                $item->field_value = AttachmentService::getReponsePicture($item->field_value);
            }
            // 图片处理
            switch ($item->module) {
                case 'site':
                    switch ($item->field) {
                        case 'logo_image':
                            $item->field_value = AttachmentService::previewList($item->field_value);
                            break;
                    }
                    break;
            }

            $result[$item->module][$item->field] = $item->field_value;
        });
        return $this->success('', $result);
    }

    /**
     * 更新
     * @param ConfigRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(ConfigRequest $request)
    {
        $request->validate('update');
        $configData = $request->all();
        // $joinField和$jsonField字段互斥
        $joinField = ['site' => ['keywords'], 'attachment' => ['attachment_upload_ext'], 'guestbook' => ['keywords']]; // 数值为join拼接而成的字段
        $jsonField = ['site' => ['qrcode_image']]; // json字符串存储
        Config::chunkById(10, function ($data) use ($joinField, $configData, $jsonField) {
            foreach ($data as $config) {
                $field_value = $configData[$config->module][$config->field];
                // join字符串
                if (isset($joinField[$config->module]) && in_array($config->field, $joinField[$config->module])) {
                    $field_value = join(',', array_filter($field_value));
                }
                // json图集字段
                $image_list_ids = ''; // 图集id集;
                if (isset($jsonField[$config->module]) && in_array($config->field, $jsonField[$config->module])) {
                    $getRequestPicture = AttachmentService::getRequestPicture($field_value);
                    $image_list_ids = $getRequestPicture['image_list_ids'];
                    $field_value = $getRequestPicture['image_list'];
                }
                $config->where(['module' => $config->module, 'field' => $config->field])
                    ->update(['field_value' => $field_value]);
                // 更新附件
                switch ($config->module) {
                    case 'site':
                        switch ($config->field) {
                            case 'logo_image':
                                AttachmentService::bind($config, 'site.' . $config->field, $field_value);
                                break;
                            case 'qrcode_image':
                                AttachmentService::bind($config, 'site.' . $config->field, $image_list_ids);
                                break;
                        }
                        break;
                }
            }
        }, 'id');
        return $this->successUpdated();
    }


    /**
     * 获得站点配置
     * @return \Illuminate\Http\JsonResponse
     */
    public function siteConfig()
    {
        $configs = Config::where('module', 'site')->whereIn('field', ['name', 'domain'])->get();
        $result = [];
        collect($configs)->each(function ($item) use (&$result) {
            $result[$item->field] = $item->field_value;
        });
        return $this->success('', $result);
    }

}
